package git.soulbgm.zhilian;

import git.soulbgm.utils.ExcelUtil;
import git.soulbgm.utils.LogUtil;
import git.soulbgm.utils.PropertiesUtil;
import git.soulbgm.zhilian.pojo.DeliveringSuccess;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

/**
 * 爬取智联招聘我的投递信息
 *
 * @author SoulBGM
 * @date 2021/10/19 19:38
 */
public class ZhiLianApplication {

    private static LogUtil log = new LogUtil(ZhiLianApplication.class);

    private static String username;
    private static String password;
    private static WebDriver driver;
    private static String path;
    private static ChromeOptions options;
    private static String zhilianzhaopingLoginUrl = "https://passport.zhaopin.com/login?bkUrl=%2F%2Fi.zhaopin.com%2Fblank%3Fhttps%3A%2F%2Fwww.zhaopin.com%2F";
    private static String zhilianzhaopingMeUrl = "https://i.zhaopin.com/schedule";

    static {
        try {
            detectionEnvironment();
        } catch (IOException e) {
            log.error("环境检测出现问题", e);
        }
    }

    /**
     * 判断运行时是否是以jar运行
     *
     * @return boolean
     */
    private static boolean isJar(String path) {
        File file = new File(path);
        return file.isFile();
    }

    /**
     * 检测环境
     */
    private static void detectionEnvironment() throws IOException {
        path = ZhiLianApplication.class.getProtectionDomain().getCodeSource().getLocation().getPath();
        String configFilePath = "config.properties";
        if (isJar(path)) {
            path = path.substring(0, path.lastIndexOf(PropertiesUtil.LEFT_SLANTING_BAR));
            configFilePath = path + File.separator + "config.properties";
        }
        Map<String, String> config = PropertiesUtil.getPrefixProperties(configFilePath, "config", "#");
        System.setProperty("webdriver.chrome.driver", config.get("chromeDriverPath"));
        username = config.get("zhiLianUsername");
        password = config.get("zhiLianPassword");
    }

    public static void main(String[] args) throws InterruptedException, ParseException {
        options = new ChromeOptions();
        options.addArguments("--mute-audio");
        options.addArguments("--hide-scrollbars");
        options.addArguments("disable-infobars");

        driver = new ChromeDriver(options);
        driver.get(zhilianzhaopingLoginUrl);
        driver.findElement(By.className("zppp-panel-qrcode-bar__triangle")).click();
        Thread.sleep(100);
        driver.findElement(By.xpath("//li[@class='zppp-panel-tab'][last()]")).click();
        List<WebElement> input = driver.findElements(By.tagName("input"));
        //用户名
        input.get(0).sendKeys(username);
        //密码
        input.get(1).sendKeys(password);
        //点击登录
        driver.findElement(By.className("zppp-submit")).click();
        Scanner scanner = new Scanner(System.in);
        String result = scanner.nextLine();
        driver.get(zhilianzhaopingMeUrl);

        List<DeliveringSuccess> list = getDeliveringSuccessesList();
        WebElement nextElement = driver.findElement(By.className("ivu-page-next"));
        String nextClass = nextElement.getAttribute("class");
        while (!nextClass.contains("ivu-page-disabled")) {
            nextElement.click();
            Thread.sleep(1000);
            list.addAll(getDeliveringSuccessesList());
            nextElement = driver.findElement(By.className("ivu-page-next"));
            nextClass = nextElement.getAttribute("class");
        }
        ExcelUtil.modelWriterExcel(path, "统计投递成功", list, DeliveringSuccess.class);
        log.info("导出成功");
        driver.close();
    }

    private static final SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * 获得投递成功列表
     *
     * @return {@link List<DeliveringSuccess>}
     */
    private static List<DeliveringSuccess> getDeliveringSuccessesList() throws ParseException {
        List<WebElement> elements = driver.findElements(By.className("ji-item"));
        List<DeliveringSuccess> list = new ArrayList<>(elements.size());
        for (WebElement element : elements) {
            WebElement info = element.findElement(By.className("ji-item-info"));
            DeliveringSuccess d = new DeliveringSuccess();
            d.setJobName(info.findElement(By.className("ji-item-info-jobName")).getText());
            d.setWage(info.findElement(By.tagName("p")).getText());
            WebElement companyNameElement = info.findElement(By.className("ji-item-info-companyName"));
            d.setCompanyName(companyNameElement.getText());
            d.setRecruitmentAddress(companyNameElement.getAttribute("href"));
            WebElement status = element.findElement(By.className("ji-item-status"));
            List<WebElement> span = status.findElement(By.className("ji-item-status-timer")).findElements(By.tagName("span"));
            d.setDeliveringTime(SDF.parse(span.get(0).getText() + " " + span.get(1).getText()));
            list.add(d);
        }
        return list;
    }


}
